home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- // Chapter 9: Sprites and Animation
- //
- // CAnimSprite Source File
- //
- // This file includes the CAnimSprite class implementation.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "CSprite.h"
- #include "CAnimSprite.h"
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite Constructor
- //
- // This function is called when the class is instantiated.
- //
- CAnimSprite::CAnimSprite(HDC hdc): CSprite(hdc)
- {
- iCurFrame = 0;
- iTotalFrames = 0;
-
- for (int n = 0; n < NUM_FRAMES; n++)
- sprites[n] = new CBitmap(hdc);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite Destructor
- //
- // This function is called when the class is terminated.
- //
- CAnimSprite::~CAnimSprite()
- {
- for (int n = 0; n < NUM_FRAMES; n++)
- delete sprites[n];
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::SetImageSize
- //
- // Sets the size of each animated sprite for loading.
- //
- void CAnimSprite::SetImageSize(int iImgWidth, int iImgHeight)
- {
- iWidth = iImgWidth;
- iHeight = iImgHeight;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::LoadTile
- //
- // Loads sprite from a portion of a tiled bitmap image.
- //
- BOOL CAnimSprite::LoadTile(int iTileNum, LPTSTR lpFilename, int iWidth, int iHeight)
- {
- CBitmap *source;
- int iStartX, iStartY;
-
- //make sure tile number is inside range
- if (iTileNum < 0 || iTileNum > NUM_FRAMES)
- return FALSE;
-
- //make sure sprite is not too big
- if (iWidth < 1 || iWidth > 240 || iHeight < 1 || iHeight > 320)
- return FALSE;
-
- //load the source image
- source = new CBitmap(GetScreenDC());
- if (!source->Load(lpFilename))
- {
- delete source;
- return FALSE;
- }
-
- //calculate the tile position
- iStartX = iTileNum * (iWidth + 1);
- iStartY = 0;
-
- //create the new sprite frame
- if (!sprites[iTileNum]->Create(iWidth, iHeight))
- {
- delete source;
- return FALSE;
- }
-
- //copy to the new sprite frame
- BitBlt(sprites[iTileNum]->GetSourceDC(),
- 0, 0, iWidth, iHeight,
- source->GetSourceDC(),
- iStartX, iStartY, SRCCOPY);
-
- //prepare new frame for use
- iTotalFrames++;
- SetFrame(iTileNum);
- SetImageSize(iWidth, iHeight);
- delete source;
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::LoadAnimSeq
- //
- // Loads an animation sequence from a bitmap into the frames of an
- // animated sprite. Columns and rows are frames, not pixels. Does
- // not support wrapped animation sequences: make each sequence
- // stay on a single row.
- //
- BOOL CAnimSprite::LoadAnimSeq(int iStartCol, int iNumFrames, int iRow, LPTSTR lpFilename, int iWidth, int iHeight)
- {
- CBitmap *source;
- int iStartX, iStartY, iFrame;
-
- if (iStartCol < 0 || iNumFrames > NUM_FRAMES)
- return FALSE;
-
- if (iWidth < 1 || iWidth > 240 || iHeight < 1 || iHeight > 320)
- return FALSE;
-
- source = new CBitmap(GetScreenDC());
- if (!source->Load(lpFilename))
- {
- delete source;
- return FALSE;
- }
-
- for (iFrame = 0; iFrame < iNumFrames; iFrame++)
- {
- if (!sprites[iFrame]->Create(iWidth, iHeight))
- {
- delete source;
- return FALSE;
- }
-
- iStartX = iStartCol * (iWidth + 1) + iFrame * (iWidth + 1);
- iStartY = iRow * (iHeight + 1);
-
- BitBlt(sprites[iFrame]->GetSourceDC(),
- 0, 0, iWidth, iHeight,
- source->GetSourceDC(),
- iStartX, iStartY, SRCCOPY);
-
- iTotalFrames++;
- }
-
- SetFrame(iStartCol);
- SetImageSize(iWidth, iHeight);
- delete source;
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::BitBlit
- //
- // Draws the sprite to the destination DC already specified.
- //
- BOOL CAnimSprite::BitBlit()
- {
- return BitBlt(GetScreenDC(), GetX(), GetY(),
- ImageWidth(), ImageHeight(),
- sprites[iCurFrame]->GetSourceDC(), 0, 0, SRCCOPY);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::StretchBlit
- //
- // Draws a scaled sprite to the destination.
- //
- BOOL CAnimSprite::StretchBlit(int dx, int dy)
- {
- return StretchBlt(GetScreenDC(),
- GetX(), GetY(), dx, dy,
- sprites[iCurFrame]->GetSourceDC(),
- 0, 0, ImageWidth(), ImageHeight(), SRCCOPY);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::TransBlit
- //
- // Draws the current sprite frame transparently.
- //
- BOOL CAnimSprite::TransBlit(COLORREF clrTrans)
- {
- return TransparentImage(GetScreenDC(),
- GetX(), GetY(),
- ImageWidth(), ImageHeight(),
- sprites[iCurFrame]->GetSourceDC(),0, 0,
- ImageWidth(), ImageHeight(), clrTrans);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::SetFrame
- //
- // Sets the current frame of animation.
- //
- void CAnimSprite::SetFrame(int iFrame)
- {
- if (iFrame >= 0 && iFrame < iTotalFrames)
- iCurFrame = iFrame;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::NextFrame
- //
- // Increments the current frame and accounts for the boundaries.
- //
- void CAnimSprite::NextFrame()
- {
- if (iCurFrame < iTotalFrames - 1)
- iCurFrame++;
- else
- iCurFrame = 0;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CAnimSprite::PrevFrame
- //
- // Decrements the current frame and accounts for the boundaries.
- //
- void CAnimSprite::PrevFrame()
- {
- if (iCurFrame > 0)
- iCurFrame--;
- else
- iCurFrame = iTotalFrames - 1;
- }
-
-